package org.flexspeedup.blog.views
{
import flash.events.MouseEvent;

import org.flexspeedup.blog.events.ClosePopUpEvent;
import org.flexspeedup.blog.models.PopUpModel;
import org.swizframework.core.IDispatcherAware;

import spark.components.Button;
import spark.components.Label;
import spark.components.supportClasses.SkinnableComponent;

[SkinState("ok")]
[SkinState("okCancel")]
/**
 * Component logic for pop ups
 */
public class PopUp extends SkinnableComponent implements IDispatcherAware
{

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Creates a new instance of this class.
     */
    public function PopUp()
    {
        super();
    }
    
    //--------------------------------------------------------------------------
    //
    //  Skin parts
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  titleLabel
    //----------------------------------
    [SkinPart(required="true")]
    public var titleLabel:Label;

    //----------------------------------
    //  messageLabel
    //----------------------------------
    [SkinPart(required="true")]
    public var messageLabel:Label;

    //----------------------------------
    //  okButton
    //----------------------------------
    [SkinPart(required="true")]
    public var okButton:Button;

    //----------------------------------
    //  cancelButton
    //----------------------------------
    [SkinPart(required="true")]
    public var cancelButton:Button;
    
    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  model
    //----------------------------------
    /**
     *  @private
     */    
    private var _model:PopUpModel;
    
    [Bindable]
    /**
     * The data used to render this popup
     */
    public function get model():PopUpModel
    {
        return _model;
    }
    
    public function set model(value:PopUpModel):void
    {
        _model = value;
        invalidateSkinState();
    }

    //----------------------------------
    //  dispatcher
    //----------------------------------
    /**
     *  @private
     */    
    private var _dispatcher:IEventDispatcher;
    
    /**
     * Swiz dispatcher
     */
    public function get dispatcher():IEventDispatcher
    {
        return _dispatcher;
    }
    
    public function set dispatcher(value:IEventDispatcher):void
    {
        _dispatcher = value;
    }

    
    //--------------------------------------------------------------------------
    //
    //  Overridden methods - SkinnableComponent
    //
    //--------------------------------------------------------------------------

    /**
     *  @private
     */
    override protected function getCurrentSkinState():String
    {
        var state:String;
        if (model && model.nextEvent)
            state =  "okCancel";
        else 
            state = "ok";

        return state;
    }

    /**
     *  @private
     */
    override protected function partAdded(partName:String, instance:Object):void
    {
        switch (instance)
        {
            case (okButton):
            {
                okButton.addEventListener(
                    MouseEvent.CLICK, okButton_clickHandler);
                break;
            }
            
            case (cancelButton):
            {
                cancelButton.addEventListener(
                    MouseEvent.CLICK, cancelButton_clickHandler);
                break;
            }
        }
    }

    //--------------------------------------------------------------------------
    //
    //  Event handlers
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  okButton_clickHandler
    //----------------------------------
    /**
     * @private
     */
    private function okButton_clickHandler(event:MouseEvent):void
    {
        var e:ClosePopUpEvent;
        if (model.nextEvent)
            e = new ClosePopUpEvent(model.nextEvent, true);
        else
            e = new ClosePopUpEvent();
        dispatcher.dispatchEvent(e);
    }

    //----------------------------------
    //  cancelButton_clickHandler
    //----------------------------------
    /**
     * @private
     */
    private function cancelButton_clickHandler(event:MouseEvent):void
    {
        dispatcher.dispatchEvent(new ClosePopUpEvent());
    }
}
}